<?php

namespace app\api\controller;

use app\common\controller\Api;

/**
 * 公益活动
 */
class Gyhd extends Common
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRiaght = '*';
	protected $gyhdModel = null;
    protected $gyldCategoryModel = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->gyhdModel = new \app\admin\model\Gyhd();
        $this->gyhdCategoryModel =new \app\admin\model\GyhdCategory();
		$this->userModel = new \app\admin\model\User();
    }
    /**
     *公益活动分类列表
     *
     */
    public function getgyhdCategoryList()
    {
        $gyhdCategory = $this->gyhdCategoryModel
            ->select();
        $this->outputData($gyhdCategory);
    }
    /**
     * 公益活动列表
     */
    public function getgyhdList()
    {
        $page = $this->request->get('page') - 1;
        $page=$page>0?$page:0;
        $limit = $this->request->get('pageSize');
        $limit=$limit>0?$limit:5;
        $start = $page * $limit;
        $catid=$this->request->get('catid');
        //按分类
        if($catid){
            $where['catid']=$catid;
        }
        $gyhdList = $this->gyhdModel
            ->alias('f')
            ->join('GyhdCategory fc','f.catid=fc.id')
            ->field('fc.name,f.*')
            ->where('f.jifen','<',1)
            ->limit($start,$limit)
            ->select();
        foreach ($gyhdList as &$value){
            $value['zhuangtai']=str_replace([0,1,],['未审核','已审核'],$value['status']);
        }
        $this->outputData($gyhdList);
    }
    /**
     * 公益活动
     */
    public function getgyhdDetail()
    {
        $id=$this->request->get('id');
        if(!is_numeric($id)){
            $this->outputError('参数错误');
        }
        $gyhdList = $this->gyhdModel
            ->alias('f')
            ->join('GyhdCategory fc','f.catid=fc.id')
            ->where('f.id',$id)
            ->find();
        $gyhdList['zhuangtai']=str_replace([0,1,],['未审核','已审核'],$gyhdList['status']);
        $this->outputData($gyhdList);
    }
    /**
     * 申报活动列表
     */
    public function getgyhdLists()
    {
        $page = $this->request->get('page') - 1;
        $page=$page>0?$page:0;
        $limit = $this->request->get('pageSize');
        $limit=$limit>0?$limit:5;
        $start = $page * $limit;
        $catid=$this->request->get('catid');
        $uid=$this->request->get('uid');
        //按分类
        if($catid){
            $where['catid']=$catid;
        }
        //按用户分类
        if($uid){
            $where['uid']=$uid;
        }
        $gyhdList = $this->gyhdModel
            ->alias('f')
            ->join('GyhdCategory fc','f.catid=fc.id')
            ->field('fc.name,f.*')
            ->where('f.jifen','>',0)
            ->where('f.uid',$uid)
            ->limit($start,$limit)
            ->select();
        foreach ($gyhdList as &$value){
            $value['zhuangtai']=str_replace([0,1,],['未审核','已审核'],$value['status']);
        }
        $this->outputData($gyhdList);
    }
	/**
     * 申报活动
     */
    public function getgyhdadd()
    {
        $uid=$this->request->request('uid');
        $author=$this->request->request('author');
        $catid=$this->request->request('catid');
        $description=$this->request->request('description');
        $inputtime=$this->request->request('inputtime');
        $image=$this->request->request('image');
        if(is_array($image)) {
            $image = join(',', $image);
        }
        if(!$uid||!$author||!$description||!$inputtime||!$catid){
            $this->outputError('请填写完整的信息');
        }
        $jifens= $this->gyhdCategoryModel->where('id', $catid)->find();
		$jifen=$jifens['jifen'];
        $data=['uid'=>$uid,'author'=>$author,'catid'=>$catid,'jifen'=>$jifen,'description'=>$description,'inputtime'=>time(),'image'=>$image];
        $re = $this->gyhdModel
            ->insert($data);
        if($re){
            $this->outputData('申报成功');
        }else{
            $this->outputData('申报失败');
        }
    }
   
    /**
     *公益活动审核
     */
    public function getgyhdUpdate()
    {
        $uid=$this->request->request('uid');
        $id=$this->request->request('id');
        if(!is_numeric($id)){
            $this->outputError('参数错误');
        }
		//积分
        $res=$this->gyhdModel->where('id',$id)->find();
        $cuid=$res['uid'];
        $user=$this->userModel->where('id',$cuid)->find();
        $jifen=$user['score']+$res['jifen'];
        $data=['score'=>$jifen];
        $re = $this->gyhdModel
            ->where('id',$id)
            ->update(['operator'=>$uid,'status'=>1]);
        if($re){
            $user=$this->userModel
                ->where('id', $cuid)
                ->update($data);
        }        
        if($re){
            $this->outputData('审核成功');
        }else{
            $this->outputError('审核失败');
        }
    }
}